#include "main.h"

struct MemoryStruct{
	char *Memory = NULL;
	size_t size = 0;
};

size_t WriteMemory(void *content,size_t size,size_t nmemb,void *userp){
	size_t realsize = size*nmemb;
	struct MemoryStruct *mem = (struct MemoryStruct *)userp;
	char *ptr = static_cast<char *>(realloc(mem->Memory,mem->size+realsize+1));
	mem->Memory = ptr;
	memcpy(&(mem->Memory[mem->size]),content,realsize);
	mem->size += realsize;
	mem->Memory[mem->size] = 0;
	return realsize;
	
}
const char *Get_Source(const char *home_Url){
	struct MemoryStruct content;
	struct curl_slist *Header = NULL;
	//设置Header
	//Header = curl_slist_append(Header,"Referer:https://alpha.wallhaven.cc");
	//Header = curl_slist_append(Header,"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");

	curl_global_init(CURL_GLOBAL_ALL);
	CURL *curl = curl_easy_init();
	curl_easy_setopt(curl,CURLOPT_HTTPHEADER,Header);
	curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
	curl_easy_setopt(curl,CURLOPT_URL,home_Url);
	curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteMemory);
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,&content);
	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
	curl_global_cleanup();
	free(content.Memory);
	return content.Memory;
}
